<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Unsafe #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE NoImplicitPrelude, MagicHash, UnboxedTuples #-}</span><span>
</span><span id="line-3"></span><span class="hs-pragma">{-# OPTIONS_GHC -funbox-strict-fields #-}</span><span>
</span><span id="line-4"></span><span class="hs-pragma">{-# OPTIONS_HADDOCK not-home #-}</span><span>
</span><span id="line-5"></span><span>
</span><span id="line-6"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- Module      :  GHC.MVar</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow 2008</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- License     :  see libraries/base/LICENSE</span><span>
</span><span id="line-11"></span><span class="hs-comment">--</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Maintainer  :  cvs-ghc@haskell.org</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Stability   :  internal</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- Portability :  non-portable (GHC Extensions)</span><span>
</span><span id="line-15"></span><span class="hs-comment">--</span><span>
</span><span id="line-16"></span><span class="hs-comment">-- The MVar type</span><span>
</span><span id="line-17"></span><span class="hs-comment">--</span><span>
</span><span id="line-18"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-19"></span><span>
</span><span id="line-20"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.MVar</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-21"></span><span>        </span><span class="annot"><span class="hs-comment">-- * MVars</span></span><span>
</span><span id="line-22"></span><span>          </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier">MVar</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span>
</span><span id="line-23"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#newMVar"><span class="hs-identifier">newMVar</span></a></span><span>
</span><span id="line-24"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#newEmptyMVar"><span class="hs-identifier">newEmptyMVar</span></a></span><span>
</span><span id="line-25"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#takeMVar"><span class="hs-identifier">takeMVar</span></a></span><span>
</span><span id="line-26"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#readMVar"><span class="hs-identifier">readMVar</span></a></span><span>
</span><span id="line-27"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#putMVar"><span class="hs-identifier">putMVar</span></a></span><span>
</span><span id="line-28"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#tryTakeMVar"><span class="hs-identifier">tryTakeMVar</span></a></span><span>
</span><span id="line-29"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#tryPutMVar"><span class="hs-identifier">tryPutMVar</span></a></span><span>
</span><span id="line-30"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#tryReadMVar"><span class="hs-identifier">tryReadMVar</span></a></span><span>
</span><span id="line-31"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#isEmptyMVar"><span class="hs-identifier">isEmptyMVar</span></a></span><span>
</span><span id="line-32"></span><span>        </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#addMVarFinalizer"><span class="hs-identifier">addMVarFinalizer</span></a></span><span>
</span><span id="line-33"></span><span>    </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-34"></span><span>
</span><span id="line-35"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Base.html"><span class="hs-identifier">GHC.Base</span></a></span><span>
</span><span id="line-36"></span><span>
</span><span id="line-37"></span><span class="hs-keyword">data</span><span> </span><span id="MVar"><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-var">MVar</span></a></span></span><span> </span><span id="local-6989586621679534145"><span class="annot"><a href="#local-6989586621679534145"><span class="hs-identifier hs-type">a</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="MVar"><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-var">MVar</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#MVar%23"><span class="hs-identifier hs-type">MVar#</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#RealWorld"><span class="hs-identifier hs-type">RealWorld</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534145"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-38"></span><span class="hs-comment">{- ^
An 'MVar' (pronounced \&quot;em-var\&quot;) is a synchronising variable, used
for communication between concurrent threads.  It can be thought of
as a box, which may be empty or full.
-}</span><span>
</span><span id="line-43"></span><span>
</span><span id="line-44"></span><span class="hs-comment">-- pull in Eq (Mvar a) too, to avoid GHC.Conc being an orphan-instance module</span><span>
</span><span id="line-45"></span><span class="hs-comment">-- | @since 4.1.0.0</span><span>
</span><span id="line-46"></span><span id="local-6989586621679534154"><span class="hs-keyword">instance</span><span> </span><span id="local-6989586621679534104"><span class="annot"><a href="../../ghc-prim/src/GHC.Classes.html#Eq"><span class="hs-identifier hs-type">Eq</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534154"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-47"></span><span>        </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534102"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534102"><span class="hs-identifier hs-var">mvar1#</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679534101"><span class="annot"><span class="annottext">== :: MVar a -&gt; MVar a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3D%3D"><span class="hs-operator hs-var hs-var hs-var hs-var">==</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534100"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534100"><span class="hs-identifier hs-var">mvar2#</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Int# -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#isTrue%23"><span class="hs-identifier hs-var">isTrue#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MVar# RealWorld a -&gt; MVar# RealWorld a -&gt; Int#
forall d a. MVar# d a -&gt; MVar# d a -&gt; Int#
</span><a href="../../ghc-prim/src/GHC.Prim.html#sameMVar%23"><span class="hs-identifier hs-var">sameMVar#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534102"><span class="hs-identifier hs-var">mvar1#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534100"><span class="hs-identifier hs-var">mvar2#</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-48"></span><span>
</span><span id="line-49"></span><span class="hs-comment">{-
M-Vars are rendezvous points for concurrent threads.  They begin
empty, and any attempt to read an empty M-Var blocks.  When an M-Var
is written, a single blocked thread may be freed.  Reading an M-Var
toggles its state from full back to empty.  Therefore, any value
written to an M-Var may only be read once.  Multiple reads and writes
are allowed, but there must be at least one read between any two
writes.
-}</span><span>
</span><span id="line-58"></span><span>
</span><span id="line-59"></span><span class="hs-comment">--Defined in IOBase to avoid cycle: data MVar a = MVar (SynchVar# RealWorld a)</span><span>
</span><span id="line-60"></span><span>
</span><span id="line-61"></span><span class="hs-comment">-- |Create an 'MVar' which is initially empty.</span><span>
</span><span id="line-62"></span><span id="local-6989586621679534151"><span class="annot"><a href="GHC.MVar.html#newEmptyMVar"><span class="hs-identifier hs-type">newEmptyMVar</span></a></span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534151"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-63"></span><span id="newEmptyMVar"><span class="annot"><span class="annottext">newEmptyMVar :: forall a. IO (MVar a)
</span><a href="GHC.MVar.html#newEmptyMVar"><span class="hs-identifier hs-var hs-var">newEmptyMVar</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, MVar a #)) -&gt; IO (MVar a)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, MVar a #))
 -&gt; IO (MVar a))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, MVar a #))
-&gt; IO (MVar a)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679534098"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534098"><span class="hs-identifier hs-var">s#</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-64"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, MVar# RealWorld a #)
forall d a. State# d -&gt; (# State# d, MVar# d a #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#newMVar%23"><span class="hs-identifier hs-var">newMVar#</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534098"><span class="hs-identifier hs-var">s#</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-65"></span><span>         </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534097"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534097"><span class="hs-identifier hs-var">s2#</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534096"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534096"><span class="hs-identifier hs-var">svar#</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534097"><span class="hs-identifier hs-var">s2#</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a -&gt; MVar a
forall a. MVar# RealWorld a -&gt; MVar a
</span><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-var">MVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534096"><span class="hs-identifier hs-var">svar#</span></a></span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-66"></span><span>
</span><span id="line-67"></span><span class="hs-comment">-- |Create an 'MVar' which contains the supplied value.</span><span>
</span><span id="line-68"></span><span id="local-6989586621679534144"><span class="annot"><a href="GHC.MVar.html#newMVar"><span class="hs-identifier hs-type">newMVar</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="#local-6989586621679534144"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534144"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-69"></span><span id="newMVar"><span class="annot"><span class="annottext">newMVar :: forall a. a -&gt; IO (MVar a)
</span><a href="GHC.MVar.html#newMVar"><span class="hs-identifier hs-var hs-var">newMVar</span></a></span></span><span> </span><span id="local-6989586621679534091"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534091"><span class="hs-identifier hs-var">value</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-70"></span><span>    </span><span class="annot"><span class="annottext">IO (MVar a)
forall a. IO (MVar a)
</span><a href="GHC.MVar.html#newEmptyMVar"><span class="hs-identifier hs-var">newEmptyMVar</span></a></span><span>        </span><span class="annot"><span class="annottext">IO (MVar a) -&gt; (MVar a -&gt; IO (MVar a)) -&gt; IO (MVar a)
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b
</span><a href="GHC.Base.html#%3E%3E%3D"><span class="hs-operator hs-var">&gt;&gt;=</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679534090"><span class="annot"><span class="annottext">MVar a
</span><a href="#local-6989586621679534090"><span class="hs-identifier hs-var">mvar</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-71"></span><span>    </span><span class="annot"><span class="annottext">MVar a -&gt; a -&gt; IO ()
forall a. MVar a -&gt; a -&gt; IO ()
</span><a href="GHC.MVar.html#putMVar"><span class="hs-identifier hs-var">putMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar a
</span><a href="#local-6989586621679534090"><span class="hs-identifier hs-var">mvar</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534091"><span class="hs-identifier hs-var">value</span></a></span><span>  </span><span class="annot"><span class="annottext">IO () -&gt; IO (MVar a) -&gt; IO (MVar a)
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; m b -&gt; m b
</span><a href="GHC.Base.html#%3E%3E"><span class="hs-operator hs-var">&gt;&gt;</span></a></span><span>
</span><span id="line-72"></span><span>    </span><span class="annot"><span class="annottext">MVar a -&gt; IO (MVar a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">MVar a
</span><a href="#local-6989586621679534090"><span class="hs-identifier hs-var">mvar</span></a></span><span>
</span><span id="line-73"></span><span>
</span><span id="line-74"></span><span class="hs-comment">-- |Return the contents of the 'MVar'.  If the 'MVar' is currently</span><span>
</span><span id="line-75"></span><span class="hs-comment">-- empty, 'takeMVar' will wait until it is full.  After a 'takeMVar',</span><span>
</span><span id="line-76"></span><span class="hs-comment">-- the 'MVar' is left empty.</span><span>
</span><span id="line-77"></span><span class="hs-comment">--</span><span>
</span><span id="line-78"></span><span class="hs-comment">-- There are two further important properties of 'takeMVar':</span><span>
</span><span id="line-79"></span><span class="hs-comment">--</span><span>
</span><span id="line-80"></span><span class="hs-comment">--   * 'takeMVar' is single-wakeup.  That is, if there are multiple</span><span>
</span><span id="line-81"></span><span class="hs-comment">--     threads blocked in 'takeMVar', and the 'MVar' becomes full,</span><span>
</span><span id="line-82"></span><span class="hs-comment">--     only one thread will be woken up.  The runtime guarantees that</span><span>
</span><span id="line-83"></span><span class="hs-comment">--     the woken thread completes its 'takeMVar' operation.</span><span>
</span><span id="line-84"></span><span class="hs-comment">--</span><span>
</span><span id="line-85"></span><span class="hs-comment">--   * When multiple threads are blocked on an 'MVar', they are</span><span>
</span><span id="line-86"></span><span class="hs-comment">--     woken up in FIFO order.  This is useful for providing</span><span>
</span><span id="line-87"></span><span class="hs-comment">--     fairness properties of abstractions built using 'MVar's.</span><span>
</span><span id="line-88"></span><span class="hs-comment">--</span><span>
</span><span id="line-89"></span><span id="local-6989586621679534135"><span class="annot"><a href="GHC.MVar.html#takeMVar"><span class="hs-identifier hs-type">takeMVar</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534135"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534135"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-90"></span><span id="takeMVar"><span class="annot"><span class="annottext">takeMVar :: forall a. MVar a -&gt; IO a
</span><a href="GHC.MVar.html#takeMVar"><span class="hs-identifier hs-var hs-var">takeMVar</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534089"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534089"><span class="hs-identifier hs-var">mvar#</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a)
-&gt; (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679534088"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534088"><span class="hs-identifier hs-var">s#</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a -&gt; State# RealWorld -&gt; (# State# RealWorld, a #)
forall d a. MVar# d a -&gt; State# d -&gt; (# State# d, a #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#takeMVar%23"><span class="hs-identifier hs-var">takeMVar#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534089"><span class="hs-identifier hs-var">mvar#</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534088"><span class="hs-identifier hs-var">s#</span></a></span><span>
</span><span id="line-91"></span><span>
</span><span id="line-92"></span><span class="hs-comment">-- |Atomically read the contents of an 'MVar'.  If the 'MVar' is</span><span>
</span><span id="line-93"></span><span class="hs-comment">-- currently empty, 'readMVar' will wait until it is full.</span><span>
</span><span id="line-94"></span><span class="hs-comment">-- 'readMVar' is guaranteed to receive the next 'putMVar'.</span><span>
</span><span id="line-95"></span><span class="hs-comment">--</span><span>
</span><span id="line-96"></span><span class="hs-comment">-- 'readMVar' is multiple-wakeup, so when multiple readers are</span><span>
</span><span id="line-97"></span><span class="hs-comment">-- blocked on an 'MVar', all of them are woken up at the same time.</span><span>
</span><span id="line-98"></span><span class="hs-comment">--</span><span>
</span><span id="line-99"></span><span class="hs-comment">-- /Compatibility note:/ Prior to base 4.7, 'readMVar' was a combination</span><span>
</span><span id="line-100"></span><span class="hs-comment">-- of 'takeMVar' and 'putMVar'.  This mean that in the presence of</span><span>
</span><span id="line-101"></span><span class="hs-comment">-- other threads attempting to 'putMVar', 'readMVar' could block.</span><span>
</span><span id="line-102"></span><span class="hs-comment">-- Furthermore, 'readMVar' would not receive the next 'putMVar' if there</span><span>
</span><span id="line-103"></span><span class="hs-comment">-- was already a pending thread blocked on 'takeMVar'.  The old behavior</span><span>
</span><span id="line-104"></span><span class="hs-comment">-- can be recovered by implementing 'readMVar as follows:</span><span>
</span><span id="line-105"></span><span class="hs-comment">--</span><span>
</span><span id="line-106"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-107"></span><span class="hs-comment">--  readMVar :: MVar a -&gt; IO a</span><span>
</span><span id="line-108"></span><span class="hs-comment">--  readMVar m =</span><span>
</span><span id="line-109"></span><span class="hs-comment">--    mask_ $ do</span><span>
</span><span id="line-110"></span><span class="hs-comment">--      a &lt;- takeMVar m</span><span>
</span><span id="line-111"></span><span class="hs-comment">--      putMVar m a</span><span>
</span><span id="line-112"></span><span class="hs-comment">--      return a</span><span>
</span><span id="line-113"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-114"></span><span id="local-6989586621679534087"><span class="annot"><a href="GHC.MVar.html#readMVar"><span class="hs-identifier hs-type">readMVar</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534087"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534087"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-115"></span><span id="readMVar"><span class="annot"><span class="annottext">readMVar :: forall a. MVar a -&gt; IO a
</span><a href="GHC.MVar.html#readMVar"><span class="hs-identifier hs-var hs-var">readMVar</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534086"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534086"><span class="hs-identifier hs-var">mvar#</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a)
-&gt; (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679534085"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534085"><span class="hs-identifier hs-var">s#</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a -&gt; State# RealWorld -&gt; (# State# RealWorld, a #)
forall d a. MVar# d a -&gt; State# d -&gt; (# State# d, a #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#readMVar%23"><span class="hs-identifier hs-var">readMVar#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534086"><span class="hs-identifier hs-var">mvar#</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534085"><span class="hs-identifier hs-var">s#</span></a></span><span>
</span><span id="line-116"></span><span>
</span><span id="line-117"></span><span class="hs-comment">-- |Put a value into an 'MVar'.  If the 'MVar' is currently full,</span><span>
</span><span id="line-118"></span><span class="hs-comment">-- 'putMVar' will wait until it becomes empty.</span><span>
</span><span id="line-119"></span><span class="hs-comment">--</span><span>
</span><span id="line-120"></span><span class="hs-comment">-- There are two further important properties of 'putMVar':</span><span>
</span><span id="line-121"></span><span class="hs-comment">--</span><span>
</span><span id="line-122"></span><span class="hs-comment">--   * 'putMVar' is single-wakeup.  That is, if there are multiple</span><span>
</span><span id="line-123"></span><span class="hs-comment">--     threads blocked in 'putMVar', and the 'MVar' becomes empty,</span><span>
</span><span id="line-124"></span><span class="hs-comment">--     only one thread will be woken up.  The runtime guarantees that</span><span>
</span><span id="line-125"></span><span class="hs-comment">--     the woken thread completes its 'putMVar' operation.</span><span>
</span><span id="line-126"></span><span class="hs-comment">--</span><span>
</span><span id="line-127"></span><span class="hs-comment">--   * When multiple threads are blocked on an 'MVar', they are</span><span>
</span><span id="line-128"></span><span class="hs-comment">--     woken up in FIFO order.  This is useful for providing</span><span>
</span><span id="line-129"></span><span class="hs-comment">--     fairness properties of abstractions built using 'MVar's.</span><span>
</span><span id="line-130"></span><span class="hs-comment">--</span><span>
</span><span id="line-131"></span><span id="local-6989586621679534139"><span class="annot"><a href="GHC.MVar.html#putMVar"><span class="hs-identifier hs-type">putMVar</span></a></span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534139"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679534139"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-132"></span><span id="putMVar"><span class="annot"><span class="annottext">putMVar :: forall a. MVar a -&gt; a -&gt; IO ()
</span><a href="GHC.MVar.html#putMVar"><span class="hs-identifier hs-var hs-var">putMVar</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534084"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534084"><span class="hs-identifier hs-var">mvar#</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679534083"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534083"><span class="hs-identifier hs-var">x</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ())
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679534082"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534082"><span class="hs-identifier hs-var">s#</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-133"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a -&gt; a -&gt; State# RealWorld -&gt; State# RealWorld
forall d a. MVar# d a -&gt; a -&gt; State# d -&gt; State# d
</span><a href="../../ghc-prim/src/GHC.Prim.html#putMVar%23"><span class="hs-identifier hs-var">putMVar#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534084"><span class="hs-identifier hs-var">mvar#</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534083"><span class="hs-identifier hs-var">x</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534082"><span class="hs-identifier hs-var">s#</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-134"></span><span>        </span><span id="local-6989586621679534081"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534081"><span class="hs-identifier hs-var">s2#</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534081"><span class="hs-identifier hs-var">s2#</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-135"></span><span>
</span><span id="line-136"></span><span class="hs-comment">-- |A non-blocking version of 'takeMVar'.  The 'tryTakeMVar' function</span><span>
</span><span id="line-137"></span><span class="hs-comment">-- returns immediately, with 'Nothing' if the 'MVar' was empty, or</span><span>
</span><span id="line-138"></span><span class="hs-comment">-- @'Just' a@ if the 'MVar' was full with contents @a@.  After 'tryTakeMVar',</span><span>
</span><span id="line-139"></span><span class="hs-comment">-- the 'MVar' is left empty.</span><span>
</span><span id="line-140"></span><span id="local-6989586621679534131"><span class="annot"><a href="GHC.MVar.html#tryTakeMVar"><span class="hs-identifier hs-type">tryTakeMVar</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534131"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534131"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-141"></span><span id="tryTakeMVar"><span class="annot"><span class="annottext">tryTakeMVar :: forall a. MVar a -&gt; IO (Maybe a)
</span><a href="GHC.MVar.html#tryTakeMVar"><span class="hs-identifier hs-var hs-var">tryTakeMVar</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534080"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534080"><span class="hs-identifier hs-var">m</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, Maybe a #))
-&gt; IO (Maybe a)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, Maybe a #))
 -&gt; IO (Maybe a))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, Maybe a #))
-&gt; IO (Maybe a)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679534079"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534079"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-142"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
-&gt; State# RealWorld -&gt; (# State# RealWorld, Int#, a #)
forall d a. MVar# d a -&gt; State# d -&gt; (# State# d, Int#, a #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#tryTakeMVar%23"><span class="hs-identifier hs-var">tryTakeMVar#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534080"><span class="hs-identifier hs-var">m</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534079"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-143"></span><span>        </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534078"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534078"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">0#</span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">a
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534078"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Maybe a
forall a. Maybe a
</span><a href="GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span> </span><span class="hs-special">#)</span><span>      </span><span class="hs-comment">-- MVar is empty</span><span>
</span><span id="line-144"></span><span>        </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534077"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534077"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span>  </span><span id="local-6989586621679534076"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534076"><span class="hs-identifier hs-var">a</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534077"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">a -&gt; Maybe a
forall a. a -&gt; Maybe a
</span><a href="GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534076"><span class="hs-identifier hs-var">a</span></a></span><span>  </span><span class="hs-special">#)</span><span>      </span><span class="hs-comment">-- MVar is full</span><span>
</span><span id="line-145"></span><span>
</span><span id="line-146"></span><span class="hs-comment">-- |A non-blocking version of 'putMVar'.  The 'tryPutMVar' function</span><span>
</span><span id="line-147"></span><span class="hs-comment">-- attempts to put the value @a@ into the 'MVar', returning 'True' if</span><span>
</span><span id="line-148"></span><span class="hs-comment">-- it was successful, or 'False' otherwise.</span><span>
</span><span id="line-149"></span><span id="local-6989586621679534129"><span class="annot"><a href="GHC.MVar.html#tryPutMVar"><span class="hs-identifier hs-type">tryPutMVar</span></a></span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534129"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679534129"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span></span><span>
</span><span id="line-150"></span><span id="tryPutMVar"><span class="annot"><span class="annottext">tryPutMVar :: forall a. MVar a -&gt; a -&gt; IO Bool
</span><a href="GHC.MVar.html#tryPutMVar"><span class="hs-identifier hs-var hs-var">tryPutMVar</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534075"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534075"><span class="hs-identifier hs-var">mvar#</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679534074"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534074"><span class="hs-identifier hs-var">x</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, Bool #)) -&gt; IO Bool
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, Bool #)) -&gt; IO Bool)
-&gt; (State# RealWorld -&gt; (# State# RealWorld, Bool #)) -&gt; IO Bool
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679534073"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534073"><span class="hs-identifier hs-var">s#</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-151"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
-&gt; a -&gt; State# RealWorld -&gt; (# State# RealWorld, Int# #)
forall d a. MVar# d a -&gt; a -&gt; State# d -&gt; (# State# d, Int# #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#tryPutMVar%23"><span class="hs-identifier hs-var">tryPutMVar#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534075"><span class="hs-identifier hs-var">mvar#</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534074"><span class="hs-identifier hs-var">x</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534073"><span class="hs-identifier hs-var">s#</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-152"></span><span>        </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534072"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534072"><span class="hs-identifier hs-var">s</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">0#</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534072"><span class="hs-identifier hs-var">s</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#False"><span class="hs-identifier hs-var">False</span></a></span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-153"></span><span>        </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534071"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534071"><span class="hs-identifier hs-var">s</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-identifier">_</span></span><span>  </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534071"><span class="hs-identifier hs-var">s</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#True"><span class="hs-identifier hs-var">True</span></a></span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-154"></span><span>
</span><span id="line-155"></span><span class="hs-comment">-- |A non-blocking version of 'readMVar'.  The 'tryReadMVar' function</span><span>
</span><span id="line-156"></span><span class="hs-comment">-- returns immediately, with 'Nothing' if the 'MVar' was empty, or</span><span>
</span><span id="line-157"></span><span class="hs-comment">-- @'Just' a@ if the 'MVar' was full with contents @a@.</span><span>
</span><span id="line-158"></span><span class="hs-comment">--</span><span>
</span><span id="line-159"></span><span class="hs-comment">-- @since 4.7.0.0</span><span>
</span><span id="line-160"></span><span id="local-6989586621679534070"><span class="annot"><a href="GHC.MVar.html#tryReadMVar"><span class="hs-identifier hs-type">tryReadMVar</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534070"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534070"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-161"></span><span id="tryReadMVar"><span class="annot"><span class="annottext">tryReadMVar :: forall a. MVar a -&gt; IO (Maybe a)
</span><a href="GHC.MVar.html#tryReadMVar"><span class="hs-identifier hs-var hs-var">tryReadMVar</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534069"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534069"><span class="hs-identifier hs-var">m</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, Maybe a #))
-&gt; IO (Maybe a)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, Maybe a #))
 -&gt; IO (Maybe a))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, Maybe a #))
-&gt; IO (Maybe a)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679534068"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534068"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-162"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
-&gt; State# RealWorld -&gt; (# State# RealWorld, Int#, a #)
forall d a. MVar# d a -&gt; State# d -&gt; (# State# d, Int#, a #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#tryReadMVar%23"><span class="hs-identifier hs-var">tryReadMVar#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534069"><span class="hs-identifier hs-var">m</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534068"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-163"></span><span>        </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534067"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534067"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">0#</span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">a
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534067"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Maybe a
forall a. Maybe a
</span><a href="GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span> </span><span class="hs-special">#)</span><span>      </span><span class="hs-comment">-- MVar is empty</span><span>
</span><span id="line-164"></span><span>        </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534066"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534066"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span>  </span><span id="local-6989586621679534065"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534065"><span class="hs-identifier hs-var">a</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534066"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">a -&gt; Maybe a
forall a. a -&gt; Maybe a
</span><a href="GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679534065"><span class="hs-identifier hs-var">a</span></a></span><span>  </span><span class="hs-special">#)</span><span>      </span><span class="hs-comment">-- MVar is full</span><span>
</span><span id="line-165"></span><span>
</span><span id="line-166"></span><span class="hs-comment">-- |Check whether a given 'MVar' is empty.</span><span>
</span><span id="line-167"></span><span class="hs-comment">--</span><span>
</span><span id="line-168"></span><span class="hs-comment">-- Notice that the boolean value returned  is just a snapshot of</span><span>
</span><span id="line-169"></span><span class="hs-comment">-- the state of the MVar. By the time you get to react on its result,</span><span>
</span><span id="line-170"></span><span class="hs-comment">-- the MVar may have been filled (or emptied) - so be extremely</span><span>
</span><span id="line-171"></span><span class="hs-comment">-- careful when using this operation.   Use 'tryTakeMVar' instead if possible.</span><span>
</span><span id="line-172"></span><span id="local-6989586621679534126"><span class="annot"><a href="GHC.MVar.html#isEmptyMVar"><span class="hs-identifier hs-type">isEmptyMVar</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534126"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span></span><span>
</span><span id="line-173"></span><span id="isEmptyMVar"><span class="annot"><span class="annottext">isEmptyMVar :: forall a. MVar a -&gt; IO Bool
</span><a href="GHC.MVar.html#isEmptyMVar"><span class="hs-identifier hs-var hs-var">isEmptyMVar</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534064"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534064"><span class="hs-identifier hs-var">mv#</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, Bool #)) -&gt; IO Bool
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, Bool #)) -&gt; IO Bool)
-&gt; (State# RealWorld -&gt; (# State# RealWorld, Bool #)) -&gt; IO Bool
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span id="local-6989586621679534063"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534063"><span class="hs-identifier hs-var">s#</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-174"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
-&gt; State# RealWorld -&gt; (# State# RealWorld, Int# #)
forall d a. MVar# d a -&gt; State# d -&gt; (# State# d, Int# #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#isEmptyMVar%23"><span class="hs-identifier hs-var">isEmptyMVar#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534064"><span class="hs-identifier hs-var">mv#</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534063"><span class="hs-identifier hs-var">s#</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-175"></span><span>        </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534062"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534062"><span class="hs-identifier hs-var">s2#</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534061"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534061"><span class="hs-identifier hs-var">flg</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534062"><span class="hs-identifier hs-var">s2#</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int# -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#isTrue%23"><span class="hs-identifier hs-var">isTrue#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534061"><span class="hs-identifier hs-var">flg</span></a></span><span> </span><span class="annot"><span class="annottext">Int# -&gt; Int# -&gt; Int#
</span><a href="../../ghc-prim/src/GHC.Prim.html#%2F%3D%23"><span class="hs-operator hs-var">/=#</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">0#</span></span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-176"></span><span>
</span><span id="line-177"></span><span class="hs-comment">-- |Add a finalizer to an 'MVar' (GHC only).  See &quot;Foreign.ForeignPtr&quot; and</span><span>
</span><span id="line-178"></span><span class="hs-comment">-- &quot;System.Mem.Weak&quot; for more about finalizers.</span><span>
</span><span id="line-179"></span><span id="local-6989586621679534124"><span class="annot"><a href="GHC.MVar.html#addMVarFinalizer"><span class="hs-identifier hs-type">addMVarFinalizer</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534124"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-180"></span><span id="addMVarFinalizer"><span class="annot"><span class="annottext">addMVarFinalizer :: forall a. MVar a -&gt; IO () -&gt; IO ()
</span><a href="GHC.MVar.html#addMVarFinalizer"><span class="hs-identifier hs-var hs-var">addMVarFinalizer</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span id="local-6989586621679534060"><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534060"><span class="hs-identifier hs-var">m</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span id="local-6989586621679534059"><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, () #)
</span><a href="#local-6989586621679534059"><span class="hs-identifier hs-var">finalizer</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-181"></span><span>    </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ())
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534058"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534058"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
-&gt; ()
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #))
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Weak# () #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#mkWeak%23"><span class="hs-identifier hs-var">mkWeak#</span></a></span><span> </span><span class="annot"><span class="annottext">MVar# RealWorld a
</span><a href="#local-6989586621679534060"><span class="hs-identifier hs-var">m</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, () #)
</span><a href="#local-6989586621679534059"><span class="hs-identifier hs-var">finalizer</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534058"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534057"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534057"><span class="hs-identifier hs-var">s1</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534057"><span class="hs-identifier hs-var">s1</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-182"></span><span>
</span><span id="line-183"></span></pre></body></html>